"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LinkList = void 0;
class LinkNode {
    static create(data) {
        if (data instanceof LinkNode)
            return data;
        return new LinkNode(data);
    }
    constructor(data) {
        this.data = data;
        this.pre = null;
        this.next = null;
        this.key = '';
        if (data instanceof LinkNode)
            return data;
        this.key = (data === null || data === void 0 ? void 0 : data.key) || Math.random();
    }
}
class LinkList {
    constructor() {
        this.size = 0;
        this.head = null;
        this.current = null;
        this.ended = false;
    }
    add(node) {
        if (this.ended)
            return;
        node = new LinkNode(node);
        if (!this.head) {
            this.head = node;
        }
        else {
            node.pre = this.current;
            node.pre.next = node;
        }
        this.current = node;
        this.size += 1;
        return node;
    }
    append(key, data) {
        const insert_node = this.get(key);
        const node = new LinkNode(data);
        node.next = insert_node.next;
        node.pre = insert_node;
        insert_node.next = node;
        return node;
    }
    find(callback) {
        let node = this.head;
        while (node) {
            if (callback(node)) {
                return node;
            }
            node = node.next;
        }
    }
    get(key) {
        const item = this.find((node) => node.key === key && node);
        return item;
    }
    end() {
        if (!this.head || !this.current)
            return;
        this.current.next = this.head;
        this.head.pre = this.current;
        this.ended = true;
    }
}
exports.LinkList = LinkList;
